<?php

class EventController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	$this->_helper->acl->allow('guest');
    }

    public function indexAction()
    {
    	/*
    	$empty = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::ALL);
    	$validators = array('id' => array('Int', $empty));
	    $input = new Zend_Filter_Input(null, $validators);
	    $input->setData($this->getRequest()->getParams());
	    if($input->isValid()){
	    	$event_tbl = new Application_Model_DbTable_Event();
	    	$bet_tbl = new Application_Model_DbTable_Bet();
	    	$event = $event_tbl->GetEventsById($input->id);
	    	$this->view->event = $event->current();
	    	$bets = $bet_tbl->GetBetsById(null, null, $input->id, 3);
	    	$latest_bets = array();
	    	foreach($bets as $bet){
	    		$name = $bet->getUser()->name;
	    		$latest_bets[] = array('name' => $name, 'bet' => $bet->toArray());
	    	}
	    	$this->view->latest_bets = $latest_bets;
	    }
	    else{
	    	throw new Zend_Controller_Action_Exception('Invalid input');
	    }		
	    */
    	$this->view->title = 'Event';
		$this->view->headTitle($this->view->title, 'PREPEND');
		if($input = $this->_checkInput('id', 'event')){
			$event = new Application_Model_Event($input->id);
			$this->view->event = $event;
			$latest_bets = array();
	    	foreach($event->getBets() as $bet){
	    		$name = $bet->getUser()->name;
	    		$latest_bets[] = array('name' => $name, 'bet' => $bet);
	    	}
			$this->view->latest_bets = $latest_bets;
		}
	    else{
	    	throw new Zend_Controller_Action_Exception('Invalid input');
	    }
    }
	private function _checkInput($param, $field = null){
		$validatorsSet = array();
		$validatorsSet[] = 'Int';
		$validatorsSet[] = new Zend_Validate_GreaterThan(0);
		$validatorsSet[] = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::ALL);
		if($field){
			$validatorsSet[] = new Zend_Validate_Db_RecordExists(array('table' => $field, 'field' => $param));
		}
    	$validators = array($param => $validatorsSet);
	    $input = new Zend_Filter_Input(null, $validators);
	    $input->setData($this->getRequest()->getParams());
	    if($input->isValid()){
	    	return $input;
	    }
	    else{
	    	return false;
	    }		
	}
}